var form, table, treeTable;

$(function () {
    initLayUI(function () {
        initButtons();
    })
});

function initLayUI(cb) {
    layui.use(['table', 'form', 'treetable'], function () {
        table = layui.table;
        form = layui.form;
        treeTable = layui.treetable;

        form.render();

        renderTreeTable();

        table.on('toolbar(menuTable)', function (obj) {
            if (obj.event === 'expand') {
                treeTable.expandAll('#menuTable');
            } else if (obj.event === 'fold') {
                treeTable.foldAll('#menuTable');
            } else {
                addMenu();
            }
        });

        table.on('tool(menuTable)', function (obj) {
            if (obj.event === 'delete') {
                deleteMenu(obj);
            } else if (obj.event === 'update') {
                updateMenu(obj);
            }
        });

        cb();
    });
}

function initButtons() {
    $("#searchBtn").click(function () {
        searchTable();
    });

    $("#resetBtn").click(function () {
        form.val('form', {
            "menuName": ""
        });
        searchTable();
    });
}

function renderTreeTable() {
    treeTable.render({
        treeColIndex: 0,
        treeSpid: -1,
        treeIdName: 'menuId',
        treePidName: 'parentId',
        elem: '#menuTable',
        url: ServerURL + '/menu/list',
        toolbar: '#toolbar',
        page: false,
        where: tokenUtil.getAuthHeader(),
        cols: [[
            {field: 'menuName', minWidth: 200, title: '菜单名称'},
            {field: 'icon', minWidth: 50, title: '图标'},
            {field: 'perms', minWidth: 80, title: '权限标识'},
            {field: 'url', minWidth: 100, align: 'center', title: '菜单Url'},
            {field: 'orderNum', minWidth: 50, title: '排序'},
            {
                field: 'menuType', minWidth: 100, align: 'center', templet: function (d) {
                    if (d.menuType == 2) {
                        return '<span class="layui-badge layui-bg-gray">资源</span>';
                    }
                    if (d.menuType == 1) {
                        return '<span class="layui-badge ">菜单</span>';
                    } else {
                        return '<span class="layui-badge-rim layui-bg-blue">目录</span>';
                    }
                }, title: '类型'
            },
            {templet: '#operBar', minWidth: 150, align: 'center', title: '操作'}
        ]],
        error: function (res) {
            ajaxUtil.handleError(res);
        }
    });
}

function reloadTable() {
    renderTreeTable();
}

function searchTable() {
    var keyword = $('#menuName').val();
    var searchCount = 0;
    $('#menuTable').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
        $(this).css('background-color', 'transparent');
        var text = $(this).text();
        if (keyword !== '' && text.indexOf(keyword) >= 0) {
            $(this).css('background-color', 'rgb(146,253,73)');
            if (searchCount === 0) {
                $('.layui-tab-item.layui-show').stop(true);
                $('.layui-tab-item.layui-show').animate({scrollTop: $(this).offset().top - 150}, 500);
            }
            searchCount++;
        }
    });
    if (keyword !== '' && searchCount === 0) {
        layerUtil.error("没有匹配的结果");
    } else {
        treeTable.expandAll('#menuTable');
    }
}

function addMenu() {
    putTempData('updateMenuId');
    iframeIndex = layerUtil.iframe({
        title: "新增",
        area: ['650px', '550px'],
        content: "view/auth/menusEdit.html"
    });
}

function deleteMenu(obj) {
    layerUtil.confirm("确定要删除该菜单么?", function () {
        ajaxUtil.simplePost({
            "menuId": obj.data.menuId
        }, "/menu/delete", function () {
            reloadTable();
        });
    });
}

function updateMenu(obj) {
    putTempData('updateMenuId', obj.data.menuId);
    iframeIndex = layerUtil.iframe({
        title: "修改",
        area: ['650px', '550px'],
        content: "view/auth/menusEdit.html"
    });
}